package cn.edu.zut.hkxy.controller;

import cn.edu.zut.hkxy.pojo.AjaxResult;
import cn.edu.zut.hkxy.pojo.Downloads;
import cn.edu.zut.hkxy.pojo.DownloadsExample;
import cn.edu.zut.hkxy.pojo.Ftp;
import cn.edu.zut.hkxy.service.DocsService;
import cn.edu.zut.hkxy.service.DownloadsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/downloads")
public class DownloadsController {

    @Autowired
    private DownloadsService downloadsService;
    @Autowired
    private DocsService docsService;

    @RequestMapping("/manage")
    public String toManage(){
        return "downloadsManage";
    }

    @RequestMapping("/downloads-add")
    @ResponseBody
    public AjaxResult add(Downloads downloads, @RequestParam MultipartFile[] files) throws IOException {
        AjaxResult result=new AjaxResult();
        try {
            downloadsService.add(downloads);
            int download_id =downloads.getId();
            System.out.println(download_id);
            String name1 = files[0].getOriginalFilename();
            if (name1 == null || name1.equals("")) {
                System.out.println("没有上传文件");
                //没有文件
            } else {
                docsService.addBatch(download_id, files);
            }
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e) {
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }

    @RequestMapping("/downloads-list-json")
    @ResponseBody
    public AjaxResult pageList(@RequestParam int pageNum,@RequestParam String key){
        AjaxResult result=new AjaxResult();
        try {
            DownloadsExample example=new DownloadsExample();
            if(key==null||key.equals("")){
                //没有关键字搜索时...
            }else{
                //有关键字搜索时...
                example.createCriteria().andTitleLike("%"+key+"%");
            }
            List<Downloads> list=downloadsService.getPageList(example,pageNum);
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
            result.setData(list);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }

    @RequestMapping("/downloads-delete")
    @ResponseBody
    public AjaxResult delete(@RequestParam Integer id){
        AjaxResult result=new AjaxResult();
        try {
            downloadsService.delete(id);
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }

    @RequestMapping("/downloads-show-json/{id}")
    @ResponseBody
    public AjaxResult getById(@PathVariable Integer id){
        AjaxResult result=new AjaxResult();
        try {
            Downloads downloads=downloadsService.getWithDocsById(id);
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
            result.setData(downloads);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }
    @RequestMapping("/downloads-show/{id}")
    public String getById(@PathVariable Integer id, Model model) {
        Downloads downloads = downloadsService.getWithDocsById(id);
        System.out.println(downloads);
        model.addAttribute("downloads", downloads);
        return "downloadsShow";
    }

    //以下操作FTP
    @RequestMapping("/ftp-manage")
    public String toFtpManage(){
        return "downloadftpManage";
    }

    @RequestMapping("/ftp-list-json")
    @ResponseBody
    public AjaxResult getFtpList(){
        AjaxResult result=new AjaxResult();
        try{
            List<Ftp> ftpList=downloadsService.getFtpList();
            result.setData(ftpList);
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }
    @RequestMapping("/ftp-add")
    @ResponseBody
    public AjaxResult addFtp(Ftp ftp){
        AjaxResult result=new AjaxResult();
        try{
            downloadsService.addOneFtp(ftp);
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }
    @RequestMapping("/ftp-delete")
    @ResponseBody
    public AjaxResult deleteFtp(@RequestParam Integer id){
        AjaxResult result=new AjaxResult();
        try{
            downloadsService.deleteOneFtp(id);
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }
    @RequestMapping("/ftp-modify")
    @ResponseBody
    public AjaxResult modifyFtp(@RequestParam Integer id,Ftp ftp){
        ftp.setId(id);
        AjaxResult result=new AjaxResult();
        try{
            downloadsService.modifyOneFtp(ftp);
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }finally {
            return result;
        }
    }
}
